<template>
  <div class="app-container">
    <!--搜索-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库地址" prop="warehouseAddress">
        <el-input
          v-model="queryParams.warehouseAddress"
          placeholder="请输入仓库地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库负责人" prop="warehousePrincipal">
        <el-input
          v-model="queryParams.warehousePrincipal"
          placeholder="请输入仓库负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--功能区-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:warehouse:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:warehouse:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--表格-->
    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="仓库编号
" align="center" prop="warehouseId"/>
      <el-table-column label="仓库名称" align="center" prop="warehouseName"/>
      <el-table-column label="仓库地址" align="center" prop="warehouseAddress"/>
      <el-table-column label="仓库容量" align="center" prop="warehouseCapacity"/>
      <el-table-column label="仓库面积" align="center" prop="warehouseArea"/>
      <el-table-column label="仓库介绍" align="center" prop="warehouseExplain" show-overflow-tooltip/>
      <el-table-column label="仓库负责人" align="center" prop="warehousePrincipal"/>
      <el-table-column label="仓库负责人电话" align="center" prop="warehousePrincipalTel"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleShow(scope.row)"
            v-hasPermi="['system:warehouse:query']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:warehouse:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:warehouse:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框  ref是组件测试，好比java中bean -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form  ref="form" :model="form" :rules="rules" label-width="125px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseName">
              <el-input :disabled="disabled" v-model="form.warehouseName" placeholder="请输入仓库名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库地址" prop="warehouseAddress">
              <el-input :disabled="disabled" v-model="form.warehouseAddress" placeholder="请输入仓库地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库容量" prop="warehouseCapacity">
              <el-input :disabled="disabled" v-model="form.warehouseCapacity" placeholder="请输入仓库容量">
                <i slot="suffix" style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">m³</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库面积" prop="warehouseArea">
              <el-input :disabled="disabled" v-model="form.warehouseArea" placeholder="请输入仓库面积">
                <i slot="suffix" style="font-style:normal;margin-right: 10px; line-height: 30px; color: #1e1e1e">m²</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库负责人" prop="warehousePrincipal">
              <el-input :disabled="disabled" v-model="form.warehousePrincipal" placeholder="请输入仓库负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库负责人电话" prop="warehousePrincipalTel">
              <el-input :disabled="disabled" v-model="form.warehousePrincipalTel" placeholder="请输入仓库负责人电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="仓库介绍" prop="warehouseExplain">
              <el-input :disabled="disabled" type="textarea" v-model="form.warehouseExplain" placeholder="请输入仓库介绍"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-button type="primary" style="float: right;" @click="addRow()">添加</el-button>
      <el-table :data="form.storageLocationList" style="width: 100%" ref="tb">
        <el-table-column fixed prop="storageLocationName" label="库位名称" width="150">
          <template slot-scope="scope">
            <el-input v-on:input="change1(scope.row,scope.$index)" v-model="scope.row.storageLocationName" autocomplete="off" size="small"
                      placeholder="请输入库位名称"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="storageLocationCapacity" label="库位容量" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.storageLocationCapacity" autocomplete="off" size="small"
                      placeholder="请输入库位容量"></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="storageLocationState" label="库位状态" width="150">
          <template slot-scope="scope">
<!--            <el-input v-model="scope.row.storageLocationState" autocomplete="off" size="small"-->
<!--                      placeholder="请输入库位状态"></el-input>-->

            <el-select v-model="scope.row.storageLocationState" placeholder="请输入库位状态">
              <el-option
                v-for="dict in dict.type.warehouse_state"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="storageLocationRemark" label="库位信息" width="150">
          <template slot-scope="scope">
            <el-input v-model="scope.row.storageLocationRemark" autocomplete="off" size="small"
                      placeholder="请输入库位信息"></el-input>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="btn_del" size="mini" type="text" @click="deleteTimer(scope.$index,scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div slot="footer" class="dialog-footer" v-show="!disabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse} from "@/api/warehouse/store/store";
  import { delLocation } from "@/api/warehouse/location/location";

  export default {
    name: "Warehouse",
    dicts: ['warehouse_state'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 【请填写功能名称】表格数据
        warehouseList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //如果是详情就隐藏掉功能
        disabled: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          warehouseName: null,
          warehouseAddress: null,
          warehouseCapacity: null,
          warehouseArea: null,
          warehouseExplain: null,
          warehousePrincipal: null,
          warehousePrincipalTel: null
        },
        // 表单参数
        form: {
          storageLocationList: []
        },
        // 表单校验
        rules: {
          warehouseName: [
            {required: true, message: '请输入仓库名称', trigger: 'blur'}
          ],
          warehouseAddress: [
            {required: true, message: '请输入仓库地址', trigger: 'blur'}
          ],
          warehouseCapacity: [
            {required: true, message: '请输入仓库容量', trigger: 'blur'}
          ],
          warehouseArea: [
            {required: true, message: '请输入仓库面积', trigger: 'blur'}
          ],
          warehousePrincipal: [
            {required: true, message: '请输入仓库负责人', trigger: 'blur'}
          ],
          warehousePrincipalTel: [
            {required: true, message: '仓库负责人电话', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      change1 (row,index) {
        let checkPlan = '' + row.storageLocationName
        checkPlan = checkPlan
          .replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
          .replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
          .replace(/^\./g, '') // 保证第一个为数字而不是.
          .replace('.', '$#$')
          .replace(/\./g, '')
          .replace('$#$', '.')
        if (checkPlan.indexOf('.') < 0 && checkPlan !== '') {
          // 以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
          checkPlan = parseFloat(checkPlan) + ''
        } else if (checkPlan.indexOf('.') >= 0) {
          checkPlan = checkPlan
            .replace(/^()*(\d+)\.(\d\d).*$/, '$1$2.$3') // 只能输入两个小数
        }
        this.form.storageLocationList[index].storageLocationName = checkPlan
        console.log(this.form.storageLocationList[index].storageLocationName);
        this.$forceUpdate();
      },
      /** 查询【请填写功能名称】列表 */
      getList() {
        this.loading = true;
        listWarehouse(this.queryParams).then(response => {
          this.warehouseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          warehouseId: null,
          warehouseName: null,
          warehouseAddress: null,
          warehouseCapacity: null,
          warehouseArea: null,
          warehouseExplain: null,
          warehousePrincipal: null,
          warehousePrincipalTel: null,
          storageLocationList: []
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.warehouseId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加仓库";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.disabled = false;
        this.reset();
        const warehouseId = row.warehouseId || this.ids
        getWarehouse(warehouseId).then(response => {
          this.form = {
            ...response.data,
          }
          this.open = true;
          this.title = "修改仓库";
        });
      },
      addRow() {
        this.form.storageLocationList.push({
          storageLocationName: '',
          storageLocationCapacity: '',
          storage_location_state: '',
          warehouseId: "",
          storageLocationRemark: ''
        })
      },
      /*详情按钮*/
      handleShow(row) {
        this.open = true;
        this.disabled = true;
        const warehouseId = row.warehouseId
        getWarehouse(warehouseId).then(response =>{
          this.form = response.data;
          this.title="仓库详情"
        })
      },
      /** 提交按钮 */
      submitForm() {
        console.log(this.form)
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.warehouseId != null) {
              updateWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWarehouse(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const warehouseIds = row.warehouseId || this.ids;
        this.$modal.confirm('是否确认删除仓库编号为"' + warehouseIds + '"的数据项？').then(function () {
          return delWarehouse(warehouseIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      deleteTimer(index,row){
        this.form.storageLocationList[index]
        const storgeLocationId = this.form.storageLocationList[index].storageLocationId;
        if(storgeLocationId==null){
          this.form.storageLocationList.splice(index,1)
        }else{
          delLocation(storgeLocationId).then((response) =>{
            if(response.code==200){
              this.form.storageLocationList.splice(index,1);
            }
          })
        }
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('system/warehouse/export', {
          ...this.queryParams
        }, `warehouse_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
